<?php

namespace addons\shopro\controller;
use app\admin\model\Admin;
use app\admin\model\User;
use fast\Date;
use addons\shopro\model\Order;
use think\Controller;
use think\Db;

/**
 * 控制台
 *
 * @icon  后台定时任务
 * @remark
 */
class Crontab extends Controller
{

    protected $noNeedLogin = ["*"];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();

        if (!$this->request->isCli()) {
            //$this->error('只允许在终端进行操作!');
        }
    }
    /**
     * 积分释放任务
     */
    public function scoreToBalance()
    {
        $score_proportion = config('site.score_amount');
        $total_fee = db::name('shopro_order')
            ->alias('order')
            ->field('sum(total_fee) as total_fee')
            ->join('shopro_order_item item','order.id=item.order_id','left')
            ->where(' dispatch_status = 2 and refund_status NOT IN ( 2, 3 ) and status IN ( 1, 2 )  and order.createtime >'.strtotime(date("Y-m-d")) )
            ->find();
        $time = strtotime(date("Y-m-d"));
        $transaction_log = db('transaction_log')->where('time ='.$time)->order('id desc')->find();
        if( empty($transaction_log)){
            $total = db('transaction_log')->where('time',($time-86400))->order('id desc')->find();
            empty($total) && $total['total']= 0;
            //（累计余额+当日交易额）*交易释放比例*0.01   保留两位小数
            $score = round(($total['total']+$total_fee['total_fee'])*$score_proportion*0.01,2);
            if( $score > 0 ){
                $user_list =db('user')->field('id')->select();
                foreach ($user_list as $key=>$value){
                    $res = \addons\shopro\model\User::money($score, $value['id'], 'balance', 0);
                    \think\Log::write('ScroeExpressReason:' . json_encode(['score'=>$score,'user_id'=>$value['id']]));
                }
            }
            $data = array(
                    'user_count'=>count($user_list), //用户总数
                    'total'    =>$total['total']+$total_fee['total_fee']-$score*count($user_list), //累计余额
                    'balance'  =>$total_fee['total_fee'],  //当日已支付交易额
                    'num'      => $score*count($user_list),
                    'ext'      => json_encode(['total_fee'=>$total_fee['total_fee'],'score_proportion'=>$score_proportion,'score'=>$score]),
                    'time'     =>   $time
            );
            db('transaction_log')->insert($data);
        }else{
            \think\Log::write('ScroeExpressReason:'.date("Y-m-d").'脚本已经执行');
        }

    }
    /*
     *当日交易总额数*积分比例
    public function score(){
        $list = Order::field('user_id,sum(total_fee) as count')
            ->where('status = 1 and createtime >'.strtotime(date("Y-m-d")) )
            ->select();
        foreach ($list as $key=>$value){
            $score = intval($value->count*0.05);
            if( $score > 0){
                \addons\shopro\model\User::score($score, $value->user_id, 'release', 0);
                \think\Log::write('ScroeExpressReason:' . json_encode(['score'=>$score,'user_id'=>$value->user_id]));
            }
        }
    }*/
}
